package copyengine.ui.display.movieClip
{
    import copyengine.infoTree.meta.CESMovieClipMeta;
    import copyengine.log.CELog;

    import flash.utils.Dictionary;

    import starling.display.Sprite;

    /**
     * 该类仅提供gotoAndStop结构,并且不会自己每帧都进行播放.
     *
     * 原因如下:
     *
     * 1· CEMovieClip 的用处定位为UI Component的处理, 比如像两帧的Button等等 这些组件本身是不需要Tick的
     *  如果用于播放动画,可以由GAF替代
     *
     * 2· 加入gotoAndPlay 且自己Tick播放 代码会变得臃肿复杂 如果有需求 也采用拆分方式处理 及本类保持不变 写一个扩展类
     * 每帧的时候都去Tick这个MovieClip
     *
     */
    public class CEMovieClip extends Sprite
    {
        private var mMeta:CESMovieClipMeta;
        private var mAllSubChildMcVector:Vector.<CEMovieClipChild>;
        private var mAllSubChildMcKeyMapDic:Dictionary;

        private var mCurrentFrame:int;
        private var mTotalFrame:int;
        private var mIsDisposed:Boolean;

        public function CEMovieClip(_meta:CESMovieClipMeta, _allSubChildMcVector:Vector.<CEMovieClipChild>)
        {
            mMeta = _meta;
            mAllSubChildMcVector = _allSubChildMcVector;
            mCurrentFrame = 1
            mTotalFrame = mMeta.totalFrames;

            super();

            var allChildNum:int = mAllSubChildMcVector.length;
            for (var i:int = 0; i < allChildNum; i++) {
                this.addChild(mAllSubChildMcVector[i].getTarget());
            }

            gotoAndStop(mCurrentFrame);
        }

        override public function dispose():void
        {
            if (mIsDisposed) {
                return;
            }

            mAllSubChildMcVector = null;
            mAllSubChildMcKeyMapDic = null;
            super.dispose();
            mIsDisposed = true;
        }

        //==============================//
        //==Public Interface  Function
        //==============================//

        public function gotoAndStop(_keyFrame:*):void
        {
            if (mIsDisposed) {
                //防御性编程,因为Mc很底层 防止上次逻辑出错后直接Crash
                CELog.warn("try to call gotoAndStop function ,but CESUIMovieClip already dispose!")
                return;
            }

            var newFrameIndex:int = getKeyFrameIndex(_keyFrame);
            if (mCurrentFrame != newFrameIndex) {
                mCurrentFrame = newFrameIndex;
                refreshChildDisplay();
            }
        }

        private function refreshChildDisplay():void
        {
            var length:int = mAllSubChildMcVector.length;
            for (var i:int = 0; i < length; i++) {
                var wrap:CEMovieClipChild = mAllSubChildMcVector[i];
                wrap.changeDisplayToFrame(mCurrentFrame);
            }
        }

        private function getKeyFrameIndex(_keyFrame:*):int
        {
            if (_keyFrame is String) {
                return mMeta.mKeyAndIndexMapDic[_keyFrame as String] as int;
            }
            else if (_keyFrame is int) {
                return _keyFrame as int;
            }
            else {
                CELog.err("unknown keyFrame: " + _keyFrame);
                return 1;
            }
        }


    }
}
